package com.mspbots.core.wise.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mspbots.core.AbstractBaseController;
import com.mspbots.core.teams.model.TeamsUser;
import com.mspbots.core.teams.service.TeamsUserService;
import com.mspbots.core.wise.model.TenantUserMapping;
import com.mspbots.core.wise.model.Users;
import com.mspbots.core.wise.service.TenantUserMappingService;
import com.mspbots.core.wise.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.concurrent.Callable;

/**
 * @author Dinnes Zhang
 * @date
 */
@RestController
@RequestMapping("/users")
public class UsersController extends AbstractBaseController<Users, UsersService> {

    @Autowired
    private UsersService usersService;
    @Autowired
    private TenantUserMappingService mappingService;
    @Autowired
    private TeamsUserService teamsUserService;

    @GetMapping("{tenantId}/list-name")
    public Mono<List<Users>> listName(@PathVariable("tenantId") Long tenantId) {
        return Mono.justOrEmpty(usersService.list(Wrappers.<Users>lambdaQuery().select(Users::getId, Users::getUsername)
                .eq(Users::getTenantId, tenantId)
                .eq(Users::getEnabled, true)));
    }

    @GetMapping("{tenantId}/cwuser/{tenantUserId}")
    public Mono<Users> selectPage(@PathVariable("tenantId") Long tenantId, @PathVariable("tenantUserId") Long tenantUserId) {
//        return () -> {
        TenantUserMapping mapping = mappingService.getOne(Wrappers.<TenantUserMapping>lambdaQuery().select(TenantUserMapping::getTeamsUserId)
                .eq(TenantUserMapping::getTenantId, tenantId)
                .eq(TenantUserMapping::getTenantUserId, tenantUserId));
        return Mono.justOrEmpty(mapping)
                .map(tenantUserMapping -> teamsUserService.getOne(Wrappers.<TeamsUser>lambdaQuery()
                        .select(TeamsUser::getTeamsMail)
                        .eq(TeamsUser::getTenantId, tenantId)
                        .eq(TeamsUser::getTeamsUserId, tenantUserMapping.getTeamsUserId())))
                .switchIfEmpty(Mono.empty())
                .map(teamsUser -> usersService.getOne(Wrappers.<Users>lambdaQuery().eq(Users::getTenantId, tenantId).eq(Users::getUsername, teamsUser.getTeamsMail())))
                .switchIfEmpty(Mono.empty());

//
//            TeamsUser teamsUser = teamsUserService.getOne(Wrappers.<TeamsUser>lambdaQuery()
//                    .select(TeamsUser::getTeamsMail)
//                    .eq(TeamsUser::getTenantId, tenantId)
//                    .eq(TeamsUser::getTeamsUserId, tenantUserMapping.getTeamsUserId()));
//            return usersService.getOne(Wrappers.<Users>lambdaQuery().eq(Users::getTenantId, tenantId).eq(Users::getUsername, teamsUser.getTeamsMail()));
//        };
    }

    @GetMapping("/findByEmail/{email}")
    public Callable findByEmail(@PathVariable String email) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", email);
        return () -> this.service.getOne(queryWrapper);
    }


    @GetMapping("/disableUserNoCW")
    public Callable disableUserNoCW(Users user) {
        return () -> this.service.disableUserNoCw(user);
    }

}
